package org.etlt.load;

import org.etlt.SettingCheck;

public class ColumnSetting implements SettingCheck {

    public static final char EQUAL = '=';

    /**
     *
     * @param nameAndExpression in form of name=expression
     */
    public ColumnSetting(String nameAndExpression) {
        int index = nameAndExpression.indexOf(EQUAL);
        if (index != -1) {
            setName(nameAndExpression.substring(0, index).trim());
            setExpression(nameAndExpression.substring(index + 1).trim());
        } else
            throw new IllegalArgumentException("expression for column setting: " + nameAndExpression);
    }

    /**
     *
     * @param name
     * @param expression data from this extractor
     */
    public ColumnSetting(String name, String expression) {
        setName(name.trim());
        setExpression(expression.trim());
    }

    private String name;

    private String expression;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getExpression() {
        return expression;
    }

    public void setExpression(String expression) {
        this.expression = expression;
    }

    @Override
    public int hashCode() {
        return getName().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == null || obj.getClass()!=ColumnSetting.class){
            return false;
        }
        ColumnSetting setting = (ColumnSetting) obj;
        return getName().equals(setting.getName());
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder("ColumnSetting{");
        stringBuilder.append("name='").append(getName()).append('\'')
                .append(", expression='").append(expression).append('\'')
                .append('}');
        return stringBuilder.toString();
    }

    @Override
    public void check() {
        assertName("name", getName());
    }
}
